package com.hfzy.ihk.common.web.context;

import io.lettuce.core.dynamic.annotation.Param;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.http.HttpStatus;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2018/7/17.
 * TODO 话说，只有打war包时才需要SpringBootServletInitializer
 *
 * @author wws
 */
public abstract class AbstractGracefulShutDownServletInitializer extends SpringBootServletInitializer {


    @Bean
    public GracefulShutdown gracefulShutdown() {

        System.out.println("GracefulShutdown init...");
        return new GracefulShutdown();
    }

    @Bean
    public ConfigurableServletWebServerFactory servletContainer(/*Connector httpConnector*/) {
        System.out.println("servletContainer init...");
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();/*{

            @Override
            protected void postProcessContext(Context context) {

                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }
        };*/

//            tomcat.addAdditionalTomcatConnectors(httpConnector);

        tomcat.addConnectorCustomizers(gracefulShutdown());
//        tomcat.addAdditionalTomcatConnectors(httpConnector);
//        tomcat.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/error.html"));
        return tomcat;
    }

}
